﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookManageSystem
{
    public partial class FormUpdatePwd_Admin : Form
    {
        public FormUpdatePwd_Admin()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //需要返回的可能
            if (txtNewPwd.Text == "" || txtOldPwd.Text == "" || txtVerifyPwd.Text == "")
            {
                MessageBox.Show("请输入完整信息", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (txtNewPwd.Text != txtVerifyPwd.Text)
            {
                MessageBox.Show("两次输入的密码不一致", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //原密码与数据库中的作比较
            Dao dao = new Dao();
            dao.Concnect();
            //读取数据库中的原密码
            string sql = $"select Pwd from T_Admin where AdminID = {form1.id}";
            SqlDataReader reader = dao.read(sql);
            reader.Read();
            if (reader[0].ToString() != txtOldPwd.Text)
            {
                MessageBox.Show("原密码输入错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                reader.Close();
                dao.DaoClose();
                return;
            }

            try
            {   
                //修改
                sql = $"update T_Admin set Pwd = {txtNewPwd.Text} where AdminID= {form1.id}";
                if (dao.Execute(sql) > 0)
                {
                    reader.Close();
                    dao.DaoClose();
                    MessageBox.Show("密码修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    reader.Close();
                    dao.DaoClose();
                    MessageBox.Show("密码修改失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("密码修改失败,原因："+ ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
